package com.sdry.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sdry.base.BaseController;
import com.sdry.constant.PartsResult;
import com.sdry.constant.PartsResultConstant;
import com.sdry.entity.PartCategory;
import com.sdry.entity.PartCategoryExample;
import com.sdry.entity.PartInfo;
import com.sdry.entity.PartInfoExample;
import com.sdry.service.PartCategoryService;
import com.sdry.service.PartInfoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 备品备件
 * 
 * @AUTHOR HACKER
 */
@Api(value = "备品备件")
@Controller
@RequestMapping("/base/part")
public class PartInfoController extends BaseController {

	@Autowired
	private PartInfoService partInfoService;

	@Autowired
	private PartCategoryService partCategoryService;

	/**
	 * 首页
	 * @return
	 */
	@ApiOperation(value = "首页")
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index() {
		return "/base/part/index";
	}

	/**
	 * 列表
	 * @param offset
	 * @param limit
	 * @param search
	 * @param sort
	 * @param order
	 * @return
	 */
	@ApiOperation(value = "列表")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public Object list(@RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
			@RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
			@RequestParam(required = false, defaultValue = "", value = "search") String search,
			@RequestParam(required = false, value = "sort") String sort,
			@RequestParam(required = false, value = "order") String order) {
		PartInfoExample partInfoExample = new PartInfoExample();
		if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
			partInfoExample.setOrderByClause(sort + " " + order);
		}
		if (StringUtils.isNotBlank(search)) {
			partInfoExample.or().andModelLike("%" + search + "%");
		}
		List<PartInfo> rows = partInfoService.selectByExampleForOffsetPage(partInfoExample, offset, limit);
		long total = partInfoService.countByExample(partInfoExample);
		Map<String, Object> result = new HashMap<>();
		result.put("rows", rows);
		result.put("total", total);
		return result;
	}

	/**
	 * 新增
	 * @param modelMap
	 * @return
	 */
	@ApiOperation(value = "新增")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String create(ModelMap modelMap) {
		PartCategoryExample example = new PartCategoryExample();
		List<PartCategory> partCategories = partCategoryService.selectByExample(example);
		modelMap.put("partCategories", partCategories);
		return "/base/part/create";
	}

	/**
	 * 新增
	 * @param partInfo
	 * @return
	 */
	@ApiOperation(value = "新增")
	@ResponseBody
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public Object create(PartInfo partInfo) {
		int num = partInfoService.insert(partInfo);
		if (num == 0) {
			return new PartsResult(PartsResultConstant.FAILED, "");
		}
		return new PartsResult(PartsResultConstant.SUCCESS, 1);
	}

	/**
	 * 修改
	 * @param id
	 * @param modelMap
	 * @return
	 */
	@ApiOperation(value = "修改")
	@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
	public String update(@PathVariable("id") Long id, ModelMap modelMap) {
		PartInfo partInfo = partInfoService.selectByPrimaryKey(id);
		PartCategoryExample example = new PartCategoryExample();
		List<PartCategory> partCategories = partCategoryService.selectByExample(example);
		modelMap.put("partInfo", partInfo);
		modelMap.put("partCategories", partCategories);
		return "/base/part/update";
	}

	/**
	 * 修改
	 * @param id
	 * @param partInfo
	 * @return
	 */
	@ApiOperation(value = "修改")
	@RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
	@ResponseBody
	public Object update(@PathVariable("id") int id, PartInfo partInfo) {
		int count = partInfoService.updateByPrimaryKeySelective(partInfo);
		if (count == 0) {
			return new PartsResult(PartsResultConstant.FAILED, "");
		}
		return new PartsResult(PartsResultConstant.SUCCESS, 1);
	}

	/**
	 * 删除
	 * @param ids
	 * @return
	 */
	@ApiOperation(value = "删除")
	@RequestMapping(value = "/delete/{ids}", method = RequestMethod.GET)
	@ResponseBody
	public Object delete(@PathVariable("ids") String ids) {
		int count = partInfoService.deleteByPrimaryKeys2(ids);
		if (count == 0) {
			return new PartsResult(PartsResultConstant.FAILED, "");
		}
		return new PartsResult(PartsResultConstant.SUCCESS, 1);
	}

}
